package com.talent.web.datasource.influxdb;//package com.talent.web.datasource.influxdb;


import com.talent.web.utils.TlConstant;
import lombok.RequiredArgsConstructor;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.Random;

/**
 * @ClassName：SimulationSend
 * @Author: hszhou
 * @Date: 2024/10/31 11:04
 * @Description: 必须描述类做什么事情, 实现什么功能
 * */
@Service
@RequiredArgsConstructor
public class SimulationSendTask {


    private final  @Nullable TlInfluxdbTemplate influxService;

    @Scheduled(fixedRate = 1000)
   public void send(){

        Random random = new Random();

        ChargeDischargeGun item = new ChargeDischargeGun();
        item.setCode("1");
        item.setTradeSerialNumber("0000000");
        item.setDeviceModel(0);
        item.setDeviceState(1);

        item.setOutputCurrent(da(random));
        item.setOutputVoltage(da(random));
        item.setOutputPower(da(random));

        item.setInputCurrent(da(random));
        item.setInputVoltage(da(random));
        item.setInputPower(da(random));

        item.setThreeEyeInsertionTemperature(da(random));
        item.setBoxTemperature(da(random));

        item.setReserve1(0);
        item.setReserve2(0);
        item.setReserve3(0);
        item.setReserve4(0);

        item.setOverCurrentState(0);
        item.setInputConnectorStatus(0);
        item.setLeakageSelfCheck(0);
        item.setFireZeroPhaseErrorState(0);
        item.setGroundingDetectionStatus(0);
        item.setRelayAdhesionState(0);
        item.setThreeEyeInsertionTemperatureStatus(0);
        item.setTemperatureStatusInsideTheBox(0);
        item.setDiodeDetection(0);
        item.setLeakageState(0);
        item.setOverVoltageAndUnderVoltageState(0);
        item.setGunConnectionStatus(0);
        SseEmitter emitter = TlConstant.SSE_CACHE.get("1");
        if(emitter!=null){
            try {
              //  emitter.send(SseEmitter.event().data(item).name("sse-message"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
       //  influxService.writePojo(item, WritePrecision.MS);
    }

    public Double da(  Random random ){
        // 生成一个20到100之间的随机数
        int randomNumber = 20 + random.nextInt(81); // 因为下限是包含的，上限是不包含的，所以是20到100之间的随机数

//        // 将随机数格式化为两位小数
       String formattedNumber = String.format("%.2f", (double) randomNumber);
        return Double.valueOf(formattedNumber);
    }
}
